<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react@17.js"></script>
<script src="../react-dom@17.js"></script>
<script src="../babel.js"></script>
<script type="text/babel">
    // Component 组件
    class App extends React.Component{
        // class类组件状态属性
        state={
            name:"张三",
            age:18,
            list:["关羽","张飞","赵云"]
        }
        sex="男"
        render(){
            let {list}= this.state;
            console.log(this)
            return (
                <div>
                <h1>class类组件状态</h1> 
                <h3>{this.state.name}----{this.state.age}</h3>  
                <button onClick={()=>{
                    console.log(this)
                    this.state.age++;
                    // 更新状态  每调用一次setState render就会从新执行一次页面就会从新渲染
                    this.setState({
                       age:this.state.age,
                        name:"李四"
                    })
                }}>++</button>
                <h4>{this.sex}</h4> 
                <button onClick={()=>{
                    this.sex = '女'
                    ReactDOM.render(<App/>,document.getElementById("root"))
                }}>改变性别</button>
                <ol>
                    {
                        list.map((item,index)=>{
                            return <li key={index} onClick={()=>this.removeUser(index)}>{item}</li>
                        })
                    }    
                </ol>
                <button onClick={()=>this.addUser()}>添加</button>
                </div>
            )
        }
        addUser(){
            let {list} = this.state;
            list.push("黄忠");
            this.setState({
                list:list
            })
        }
        removeUser(index){
            let {list} = this.state;
            list.splice(index,1);
            // console.log(list)
            this.setState({
                list:list
            })
        }
    }
    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>